﻿using LogLib;
using PhotoNamingService;
using System.IO;
using System.Windows.Forms;

namespace FormPageConfigs.ChildPages
{
    public partial class PageConfigNaming : PageConfigBase
    {
        private PhotoNameConfig _option = new PhotoNameConfig();
        public PhotoNameConfig Option
        {
            get { return _option; }
            set { _option = value; }
        }

        private string[] ArrayNamingType = new string[] { "PhotoNamingDefault", "PhotoNamingCardID" };

        public PageConfigNaming()
        {
            InitializeComponent();
        }

        public override bool LoadConfig()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }

            bool bRet = _option.Load();
            if (!_option.Load())
            {
                _error = _option.ErrorMessage;
                Log.WriteAsync(_error);
            }
            return bRet;
        }

        private void InitCombox()
        {
            string typeName = _option.NamingType;
            for (int i = 0; i < ArrayNamingType.Length; i++)
            {
                if (typeName.Equals(ArrayNamingType[i]))
                {
                    metroComboBoxNamingType.SelectedIndex = i;
                    return;
                }
            }
            metroComboBoxNamingType.SelectedIndex = 0;
        }

        public override bool InitCtrls()
        {
            InitCombox();
            metroToggleEnableOverride.Checked = _option.EnableOverride;
            metroToggleShowing.Checked = _option.Showing;
       
            _bInited = true;
            return true;
        }

        public override bool UpdateData()
        {
              if(metroComboBoxNamingType.SelectedIndex == -1)
            {
                MessageBox.Show("没有选择有效的照片命名方式");
                return false;
            }
            _option.NamingType = ArrayNamingType[metroComboBoxNamingType.SelectedIndex];
            _option.EnableOverride = metroToggleEnableOverride.Checked;
            _option.Showing = metroToggleShowing.Checked;
            
            _option.Save();
            return true;
        }
    }
}
